importScripts("graphvis.layout.min.js");

onmessage = function(event) {
	var nodes = event.data.nodes,
		links = event.data.links;
		layoutType = event.data.layoutType;
		center=event.data.center||[0,0];
	
	//布局次数
	let layoutTimes=0,totalTimes=1000;
	
	var layout=new LayoutFactory({nodes:nodes,links:links}).createLayout(layoutType);
	layout.initAlgo();
	
	if(layoutType == 'grid'){//网格布局
		layout.resetConfig({horizontalScale:120,verticalScale:120,center:center});
		layout.boolTransition=false;
	}else if(layoutType == 'fastFR'){//力导向布局
		layout.resetConfig({froce:0.95,linkDistance:150,linkStrength:0.08,charge:-200,gravity:0.001,noverlap:true,size:[center[0]*2,center[1]*2]});
	}else if(layoutType == 'frDirect'){ //力导布局1
		layout.resetConfig({attraction:2,force:80});
	}else if(layoutType == 'concentric'){ //中心圆布局
		layout.resetConfig({maxNodeSize:80,levelWidth:2});
	}else if(layoutType == 'fr'){ //圆形网络布局
		var fastlayout=new LayoutFactory({nodes:nodes,links:links}).createLayout('fastFR');
		fastlayout.initAlgo();
		while(layoutTimes++ < 300){
			fastlayout.runLayout(); //执行布局算法
		}
		layout.resetConfig({area:200,gravity:6});
	}
	
	//执行n次布局
	while(layoutTimes++ < totalTimes){
		layout.runLayout(); //执行布局算法
		postMessage({type: "ticked", counter:layoutTimes});
	}
	
	postMessage({type: "completed", nodes: nodes});
	self.close();
};
